﻿using System;
using System.Collections.Generic;
using System.Data.Entity;
using System.Data.Entity.Infrastructure;
using System.Linq;
using System.Web;

namespace MvcTasks.Models
{
    public class DbContextWrapper : IDbContextWrapper , IDisposable
    {
        private TaskDBContext TaskDbContext { get; set; }
        

        public IDbContextWrapper Open(string connectionString)
        {
            TaskDbContext = new TaskDBContext(connectionString);
            return this;
        }

        public IDbContextWrapper Open()
        {
            TaskDbContext = new TaskDBContext();
            return this;
        }

        public DbSet<RandomTask> RandomTask { get { return TaskDbContext.RandomTask; } set { TaskDbContext.RandomTask = value; } }

        

        public DbSet<Genre> GenreSet
        {
            get { return TaskDbContext.Genre; }
            set { TaskDbContext.Genre = value; }
        }
        
       

        public void SaveChanges()
        {
            TaskDbContext.SaveChanges();
        }

        public void Dispose()
        {
            if (TaskDbContext != null)
            {
                TaskDbContext.Dispose();
                TaskDbContext = null;
            }
        }


        public DbEntityEntry Entry(RandomTask tasks)
        {
            return TaskDbContext.Entry(tasks);
        }

        public void Detach(object entity)
        {
           TaskDbContext.ThisObjectContext.Detach(entity);
        }
    }
}